package com.dat255.tentatime;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.Spinner;

import com.dat255.tentatime.database.TTDbAdapter;

public class TodoDetails extends Activity {
	private EditText mTitleText;
	private EditText mBodyText;
	private Long mRowId;
	private TTDbAdapter mDbHelper;
	private Spinner mCategory;

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		mDbHelper = new TTDbAdapter(this);
		mDbHelper.open();
		setContentView(R.layout.result_row);
//		mTitleText = (EditText) findViewById(R.id.todo_edit_summary);
//		mBodyText = (EditText) findViewById(R.id.todo_edit_description);

//		Button confirmButton = (Button) findViewById(R.id.todo_edit_button);
		mRowId = null;
		Bundle extras = getIntent().getExtras();
		mRowId = (bundle == null) ? null : (Long) bundle
				.getSerializable(TTDbAdapter.KEY_ROWID);
		if (extras != null) {
			mRowId = extras.getLong(TTDbAdapter.KEY_ROWID);
		}
		populateFields();
//		confirmButton.setOnClickListener(new View.OnClickListener() {
//			public void onClick(View view) {
//				setResult(RESULT_OK);
//				finish();
//			}

//		});
	}

	private void populateFields() {
		if (mRowId != null) {
			Cursor todo = mDbHelper.fetchExam(mRowId);
			startManagingCursor(todo);
			String category = todo.getString(todo
					.getColumnIndexOrThrow(TTDbAdapter.KEY_ID));
			
			for (int i=0; i<mCategory.getCount();i++){
				
				String s = (String) mCategory.getItemAtPosition(i); 
				Log.e(null, s +" " + category);
				if (s.equalsIgnoreCase(category)){
					mCategory.setSelection(i);
				}
			}
			
			mTitleText.setText(todo.getString(todo
					.getColumnIndexOrThrow(TTDbAdapter.KEY_ID)));
			mBodyText.setText(todo.getString(todo
					.getColumnIndexOrThrow(TTDbAdapter.KEY_ID)));
		}
	}

	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		saveState();
		outState.putSerializable(TTDbAdapter.KEY_ROWID, mRowId);
	}

	@Override
	protected void onPause() {
		super.onPause();
		saveState();
	}

	@Override
	protected void onResume() {
		super.onResume();
		populateFields();
	}

	private void saveState() {
		String category = (String) mCategory.getSelectedItem();
		String summary = mTitleText.getText().toString();
		String description = mBodyText.getText().toString();
		

		if (mRowId == null) {
			long id = mDbHelper.createExam(category, summary, description,null,null);
			if (id > 0) {
				mRowId = id;
			}
		} else {
			mDbHelper.updateExam(mRowId, category, summary, description,null,null);
		}
	}
}